import { Component, OnInit } from '@angular/core';
import { SearchService } from './search.service';
import * as _ from 'lodash'
import { environment } from '../../../../../environments/environment';
import { DataService } from '../../../../services/data.service';
@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.scss'],
  providers: [SearchService]
})
export class SearchComponent implements OnInit {

  public managerList: any;
  //获取字典值
  public dictionaryList = {
    FUND_CATEGORY_GM: [],
    INVEST_STYLE: [],
    EXCHANGE: []
  };
  // 基金经理多选下拉框
  public managerSelectList = [];
  // 分页列表post数据
  public getBody = {
    pageIndex: 1,
    pageSize: 10,
    sFundStatus: '',
    sFundCategory: '',//基金类别
    sBank: '',//托管行
    sCreator: '',//创建人
    // sPmAccount: '',
    sPmName: '',
    sFullName: '',
    sFundCode: '',
    sIsRegisterChg: ''
  }
  // 备案机构、受托人、托管人
  public distionary: any = {
    RECORDORG_TYPE: [],  // 备案机构
    LINK_PRINCIPAL_NO: [],   //受托人
    TRUSTEE: []   //  托管人
  }
  constructor(
    public searchService: SearchService,
    public dataService: DataService
  ) { }

  ngOnInit() {
    let that = this;
    // 多选下拉框
    window['$'](".select2").select2();
    // 字典值
    this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));

    // 托管行
    this.getManagerList();
    this.getDictionary();

  }
  loadSelect() {
    let that = this;
    window['$'](".select2").select2();
    window['$']('#sTrusteePublicSelected').select2({ width: '100%', tags: true });
    window['$']('#sTrusteePublicSelected').on("select2:select", function (e) {
      that.getBody.sBank = window['$']('#sTrusteePublicSelected').select2("val");
    })

    window['$']('#sPmNameSelected').select2({ width: '100%', tags: true });
    window['$']('#sPmNameSelected').on("select2:select", function (e) {
      that.getBody.sPmName = window['$']('#sPmNameSelected').select2("val");
    })

    window['$']('#sCreatorSelected').select2({ width: '100%', tags: true });
    window['$']('#sCreatorSelected').on("select2:select", function (e) {
      that.getBody.sCreator = window['$']('#sCreatorSelected').select2("val");
    });
  }
  // 基金经理
  getManagerList() {
    let that = this;

    this.searchService.getManagerList().subscribe(data => {
      if (data) {
        that.managerList = data;
      }
    })
  }
  // 获取备案机构、受托人、托管人下拉框值
  getDictionary() {
    let that = this;
    new Promise((resolve, reject) => {
      this.searchService.getDictionary().subscribe(data => {
        if (data) {
          that.distionary.RECORDORG_TYPE = data.RECORDORG_TYPE;
          that.distionary.LINK_PRINCIPAL_NO = data.LINK_PRINCIPAL_NO;
          that.distionary.TRUSTEE = data.TRUSTEE;
        }
        resolve(data);
      })
    }).then(data => {
      // 多选下拉框
      that.loadSelect();
    })
  }
  // // 多选下拉框基金经理Name
  // ctrlManagerName() {
  //   let that = this;
  //   let managerNameList: any;
  //   let managerChecked = [];
  //   let managerId = this.ctrlManagerId().split(",");
  //   _.forEach(managerId, item => {
  //     _.forEach(that.managerList, it => {
  //       if (item == it.sUserName) {
  //         managerChecked.push(it.sRealName);
  //       }
  //     })
  //   })
  //   managerNameList = managerChecked.join(",");
  //   return managerNameList;
  // }
  // // 多选下拉框基金经理Id
  // ctrlManagerId() {
  //   let managerIdList: any;
  //   let managerChecked = [];
  //   let selected = window['$']('.fields-select').val();
  //   _.forEach(selected, item => {
  //     let index1 = item.indexOf("'");
  //     let index2 = item.lastIndexOf("'");
  //     let manager = item.substring(index1 + 1, index2);
  //     managerChecked.push(manager);
  //   })
  //   managerIdList = managerChecked.join(",");
  //   return managerIdList;
  // }
  //发射数据
  search() {
    // this.getBody.sPmAccount = this.ctrlManagerId();
    // this.getBody.sPmName = this.ctrlManagerName();
    this.getBody.sCreator = _.trim(this.getBody.sCreator);
    this.getBody.sPmName = _.trim(this.getBody.sPmName);
    this.getBody.sBank = _.trim(this.getBody.sBank);
    console.log(this.getBody)
    this.dataService.publicSearchBody = this.getBody;
    this.dataService.publicSearchEmitter.emit(this.getBody.sFundStatus);
  }
  //重置
  reset() {
    this.getBody.sFundCategory = '';//基金类别
    this.getBody.sBank = '';//托管行
    this.getBody.sCreator = '';//创建人
    // sPmAccount: '',
    this.getBody.sPmName = '';
    this.getBody.sFullName = '';
    this.getBody.sFundCode = '';
    this.getBody.sIsRegisterChg = '';
    window['$']('#sTrusteePublicSelected').select2("val", '--请选择--');
    window['$']('#sPmNameSelected').select2("val", '--请选择--');
    window['$']('#sCreatorSelected').select2("val", '--请选择--');
    
  }
}
